package com.andy.nature.ui.adapter

/**
 * Author:Andy
 * Date:2023/3/20 19:25
 * Description:MainPagerAdapter
 */
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.andy.nature.ui.fragment.WidgetFragment
import com.andy.nature.ui.fragment.ArchitectureFragment
import com.andy.nature.ui.fragment.MoneyFragment
import com.andy.nature.ui.fragment.ToolFragment

class MainPagerAdapter(fragmentActivity: FragmentActivity) : FragmentStateAdapter(fragmentActivity) {
    companion object {
        const val HOME_PAGE = 0
        const val STRANGER_PAGE = 1
        const val FRIENDS_PAGE = 2
        const val SETTINGS_PAGE = 3
    }

    private val tabFragmentsCreators: Map<Int, () -> Fragment> = mapOf(
        HOME_PAGE to { ArchitectureFragment() },
        STRANGER_PAGE to { ToolFragment() },
        FRIENDS_PAGE to { WidgetFragment() },
        SETTINGS_PAGE to { MoneyFragment() },
    )

    override fun getItemCount(): Int {
        return tabFragmentsCreators.size
    }

    override fun createFragment(position: Int): Fragment {
        return tabFragmentsCreators[position]?.invoke() ?: throw IndexOutOfBoundsException()
    }
}
